package com.study;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson2.JSON;
import com.zhuzi.dto.sh.ShDTO.IncrCumt;
import org.junit.jupiter.api.Test;
import org.redisson.api.RAtomicLong;
import org.redisson.api.RBucket;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.redisson.codec.FstCodec;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.codec.LZ4Codec;
import org.redisson.codec.SnappyCodec;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhuzi 2024/3/31 12:20
 */
@SpringBootTest
public class AppResissonHash {
    @Resource private RedissonClient redissonClient;

    @Test
    public void setx() {
        RBucket<String> bucket = redissonClient.getBucket("anyObjecta");
        boolean b = bucket.setIfAbsent("myValue", Duration.ofSeconds(30));
        System.out.println("result" + b);
    }

    @Test
    public void setex() {
        RBucket<String> bucket = redissonClient.getBucket("anyObject");
        // 检查桶中是否存在值
        boolean exists = bucket.isExists();

        if (!exists) {
            // 如果不存在，设置值并设置过期时间
            boolean b = bucket.setIfAbsent("myValue", Duration.ofSeconds(30));

            System.out.println("Value set and expiration time is 30 seconds.");
        } else {
            System.out.println("Bucket already has a value.");
        }
    }

    @Test
    public void incr() {
        RAtomicLong atomicLong = redissonClient.getAtomicLong("myAtomicLong");
        // atomicLong.set(3);
        long l = atomicLong.incrementAndGet();
        System.out.println(l);
    }

    @Test
    public void getAndIncr() {
        RBucket<String> rBucket = redissonClient.getBucket("incrts");
        System.out.println(rBucket.get());

        RAtomicLong counter = redissonClient.getAtomicLong("myCounters");

        System.out.println("Counter incremented to: " + counter.get());

        // 执行自增操作，默认增量为1
        long newValue = counter.incrementAndGet();
        System.out.println("Counter incremented to: " + newValue);
    }

    @Test
    public void loadData() {
        RMap<String, IncrCumt> rMap = redissonClient.getMap(RedisKey.hashIncrCumt);
        System.out.println(rMap);
        Collection<IncrCumt> values = rMap.values();
        FileUtil.writeUtf8String(
                JSON.toJSONString(values), "/Users/zhuzi/Desktop/A-kzz-统计/23-03-29/file.json");
    }

    @Test
    public void code() {
        RMap<String, IncrCumt> rMap = redissonClient.getMap(RedisKey.hashIncrCumt);
        Collection<IncrCumt> list = rMap.values();
        Map<String, IncrCumt> toMap =
                list.stream()
                        .collect(
                                Collectors.toMap(k -> k.getBondId(), v -> v, (oldV, newV) -> newV));

        // 使用需要序列化的类实现序列化接口
        RMap<String, IncrCumt> rMapLz4 =
                redissonClient.getMap(RedisKey.hashIncrCumt + "LZ4Codec", new LZ4Codec());
        rMapLz4.putAll(toMap);

        RMap<String, IncrCumt> rMapSnappyCodec =
                redissonClient.getMap(RedisKey.hashIncrCumt + "SnappyCodec", new SnappyCodec());
        rMapSnappyCodec.putAll(toMap);

        RMap<String, IncrCumt> rMapTypedJsonJacksonCodec =
                redissonClient.getMap(
                        RedisKey.hashIncrCumt + "JacksonCodec", new JsonJacksonCodec());
        rMapTypedJsonJacksonCodec.putAll(toMap);

        // https://img-blog.csdnimg.cn/direct/147d4a2f069e49829b982ad89478ceef.png |FstCode体积最小
        RMap<String, IncrCumt> FstCodec =
                redissonClient.getMap(RedisKey.hashIncrCumt + "FstCodec", new FstCodec());
        FstCodec.putAll(toMap);
    }

    @Test
    public void list() {
        RBucket<List<String>> rBucket = redissonClient.getBucket("ssss");
        // 没有返回null
        System.out.println(rBucket.get());
    }

    interface RedisKey {
        String hashIncrCumt = "rs:incrcmut";
    }
}
